home *** CD-ROM | disk | FTP | other *** search
- /*
- img2ps
-
- konversion von gem-ximage-dateien nach (encapsulated) postscript
-
- geschrieben von Th. Morus Walter
-
- (c) 1994/95 by Th. Morus Walter
-
- Änderungen:
- v0.2: erweitert für beliebige bildbreiten
- einfaches gem-user-interface
- runden der auflösung falls nahe vielfachem von 50 oder 90
- */
-
- #include <aes.h>
- #include <tos.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
-
- #include <image.h>
-
- IMG_HEADER head;
- MFDB picture;
-
- int use_gem=0;
-
- void do_error(char *tos_error,char *gem_error)
- {
- if ( use_gem ) {
- form_alert(1,gem_error);
- appl_exit();
- }
- else {
- printf(tos_error);
- }
- exit(1);
- }
-
- void write_out(FILE *file,char *str)
- {
- long len,out;
-
- len=strlen(str);
-
- out=fwrite(str,sizeof(char),len,file);
-
- if ( out!=len ) {
- fclose(file);
- do_error("i/o-Error, Disk full?\n","[1][ i/o-Error | Disk full?][Ok]");
- }
- if ( ferror(file) ) {
- fclose(file);
- do_error("i/o-Error\n","[1][ i/o-Error ][Ok]");
- }
- }
-
- void write_hexbyte(char *string,char byte)
- {
- char upper,lower;
-
- upper=(unsigned char)byte/0x10;
- lower=byte&0x0F;
-
- *string++=upper<10?upper+'0':upper+'a'-10;
- *string++=lower<10?lower+'0':lower+'a'-10;
- *string=0;
- }
-
- void do_output_data(FILE *file)
- {
- int i,j,k;
- char *l;
- char str[258];
-
- for ( i=0; i<picture.fd_h; i++ ) {
- l=(char*)picture.fd_addr+picture.fd_wdwidth*2l*i;
- for ( j=k=0; j<picture.fd_w; j+=8,k+=2,l++ ) {
- write_hexbyte(str+k,~*l);
- /* sprintf(str+k,"%02x",~*l); */
- if ( k==238 ) { /* max. 240 zeichen/zeile */
- write_out(file,str);
- write_out(file,"\n");
- k=-2;
- }
- }
- write_out(file,str);
- write_out(file,"\n");
- }
- }
-
- double calc_rez(int pix_size)
- {
- double rez,rounded,error;
-
- rez=25400.0/pix_size;
-
- rounded=round(rez/50.0)*50.0;
- error=fabs(rez-rounded)/rez;
- if ( error<1e-2 )
- return rounded;
-
- rounded=round(rez/90.0)*90.0;
- error=fabs(rez-rounded)/rez;
- if ( error<1e-2 )
- return rounded;
-
- return rez;
- }
-
- int main(int argc,char *argv[])
- {
- char in[256],out[256],*c;
- char puffer[512];
- FILE *file;
- double x,y,w,h;
-
- if ( argc==1 ) { /* kein parameter übergeben */
- if ( argv[0] ) /* name verfügbar? */
- c=strrchr(argv[0],'.');
- if ( !c || stricmp(c,".ttp") )
- use_gem=1;
- else
- use_gem=1;
- }
-
- if ( !use_gem ) {
- printf("img2ps V0.2 (%s) by Th. Morus Walter\n",__DATE__);
- if ( argc<2 ) {
- printf(" usage: img2ps infile outfile\n");
- printf(" or img2ps infile\n");
- printf(" thanx to John Bradley for XV\n\n");
- return 1;
- }
- strcpy(in,argv[1]);
- }
- else {
- char path[256],name[32];
- int ret;
-
- appl_init();
- path[0]=Dgetdrv()+'A';
- path[1]=':';
- path[2]='\\';
- Dgetpath(path+2,0);
- strcat(path,"\\*.IMG");
- name[0]=0;
- fsel_input(path,name,&ret);
- if ( !ret ) {
- appl_exit();
- return 0;
- }
- strcpy(in,path);
- c=strrchr(in,'\\');
- if ( c )
- *(c+1)=0;
- strcat(in,name);
- }
-
- if ( !use_gem )
- printf("reading %s ...\n",in);
-
- if ( xload_img(&head,in,&picture)!=IMG_OK )
- do_error("load-error: img2ps aborts\n","[1][Load-error| Program aborts... ][Ok]");
-
- if ( picture.fd_nplanes!=1 )
- do_error("error: can't handle color images\n","[1][Error| can't handle color images... ][Ok]");
-
- if ( !use_gem && argc==3 )
- strcpy(out,argv[2]);
- else
- strcpy(out,in);
- c=strrchr(out,'.');
- if ( c==0 )
- strcat(out,".ps");
- else
- strcpy(c+1,"ps");
-
- if ( !use_gem )
- printf("writing %s ...\n",out);
-
- w=(double)picture.fd_w/calc_rez(head.pix_wid)*72.0;
- h=(double)picture.fd_h/calc_rez(head.pix_hght)*72.0;
-
- /* alte fassung ohne korrektur von rundungsfehlern
- w=picture.fd_w;
- w*=head.pix_wid;
- h=picture.fd_h;
- h*=head.pix_hght;
-
- w/=25400.0/72.0;
- h/=25400.0/72.0;
- */
- /* zentriert auf A4-Seite
- x=(612.0-w)/2; /* 612/72" papierbreite */
- y=(792.0-h)/2; /* 792/72" papierhöhe */
- */
- x=y=0.0; /* links unten */
-
- file=fopen(out,"w");
- if ( file ) {
- write_out(file,"%!PS-Adobe-2.0 EPSF-2.0\n");
- sprintf(puffer,"%%%%Title: %s\n",out);
- write_out(file,puffer);
- write_out(file,"%%Creator: img2ps V0.2 ("__DATE__") by Th. Morus Walter\n");
- sprintf(puffer,"%%%%BoundingBox: %d %d %d %d\n",(int)(x+.5),(int)(y+.5),(int)(x+w+.5),(int)(y+h+.5));
- write_out(file,puffer);
- write_out(file,"%%Pages: 1\n");
- write_out(file,"%%DocumentFonts:\n");
- write_out(file,"%%EndComments\n");
- write_out(file,"%%EndProlog\n");
- write_out(file,"\n");
- write_out(file,"%%Page: 1 1\n");
- write_out(file,"\n");
- write_out(file,"% remember original state\n");
- write_out(file,"/origstate save def\n");
- write_out(file,"\n");
- write_out(file,"% build a temporary dictionary\n");
- write_out(file,"20 dict begin\n");
- write_out(file,"\n");
- write_out(file,"% define string to hold a scanline's worth of data\n");
- sprintf(puffer,"/pix %d string def\n",(picture.fd_w+7)/8);
- write_out(file,puffer);
- write_out(file,"\n");
- write_out(file,"% lower left corner\n");
- sprintf(puffer,"%d %d translate\n",(int)(x+.5),(int)(y+.5));
- write_out(file,puffer);
- write_out(file,"\n");
- write_out(file,"% size of image (on paper, in 1/72inch coords)\n");
- sprintf(puffer,"%lf %lf scale\n",w,h);
- write_out(file,puffer);
-
- write_out(file,"\n");
- write_out(file,"% dimensions of data\n");
- sprintf(puffer,"%d %d %d\n",picture.fd_w,picture.fd_h,picture.fd_nplanes);
- write_out(file,puffer);
- write_out(file,"\n");
- write_out(file,"% mapping matrix\n");
- sprintf(puffer,"[%d %d %d %d %d %d]\n",picture.fd_w,0,0,-picture.fd_h,0,picture.fd_h);
- write_out(file,puffer);
- write_out(file,"\n");
-
- write_out(file,"{currentfile pix readhexstring pop}\n");
- write_out(file,"image\n");
-
- do_output_data(file);
-
- write_out(file,"\n");
- write_out(file,"showpage\n");
-
- write_out(file,"\n");
- write_out(file,"% stop using temporary dictionary\n");
- write_out(file,"end\n");
-
- write_out(file,"\n");
- write_out(file,"% restore original state\n");
- write_out(file,"origstate restore\n");
-
- write_out(file,"\n");
- write_out(file,"%% Trailer\n");
-
- fclose(file);
- if ( use_gem ) {
- char alert[256],*f1,*f2;
- f1=strrchr(in,'\\');
- if ( f1 ) f1++;
- else f1=in;
- f2=strrchr(out,'\\');
- if ( f2 ) f2++;
- else f2=out;
- sprintf(alert,"[1][IMG2PS| Converted| %s| to| %s][Ok]",f1,f2);
- form_alert(1,alert);
- }
- }
-
- free(picture.fd_addr);
-
- if ( use_gem ) {
- appl_exit();
- }
- return 0;
- }
-
-